package com.gxwzxy.medic.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.gxwzxy.medic.dao.ChronicdisDao;
import com.gxwzxy.medic.entity.Chronicdis;
import com.gxwzxy.medic.until.Connect;

public class ChronicdisDaoImpl implements ChronicdisDao {
	
	//声明连接数据库的变量
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps = null;

	/**
	 * 查询慢病个数
	 */
	@Override
	public int findChronicdisSize() {
		String sql = "select COUNT(*) from chronicdis";
		int i = 0;
		try {
			//调用静态方法
			rs = Connect.executeF(sql);
			if(rs.next()) {
				i = rs.getInt(1);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return i;
	}
	
	/**
	 * 分页查询，start是开始的页数，size是一页行有多少数据
	 */
	@Override
	public List<Chronicdis> findChronicdisIndex(int start, int size) {
		// 声明变量
		ArrayList<Chronicdis> ac = new ArrayList<>();
		String sql = null;
		try {
			// 创建Sql语句
			sql = "select * from chronicdis limit ?,?";
			// 执行sql语句
			rs =Connect.executeF(sql, start,size);
			// 获取结果
			while (rs.next()) {
				Chronicdis c = new Chronicdis(rs.getString(1), rs.getString(2), rs.getString(3),rs.getString(4));
				ac.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Connect.close(conn, ps, rs);
		}
		// 关闭连接
		return ac;
	}

	/**
	 * 保存慢病
	 */
	@Override
	public boolean saveIll(Chronicdis c) {
		//创建SQL语句
		String sql = "insert into chronicdis(illcode,illname,pycode,wbcode) value(?,?,?,?)";
		int i = Connect.executeDML(sql,c.getIllCode(),c.getIllName(),c.getPyCode(),c.getWbCode());
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据illCode修改慢病
	 */
	@Override
	public boolean updateChr(Chronicdis c) {
		//创建SQL语句
		String sql = "update chronicdis set illname = ? ,pycode = ? ,wbcode = ? where illcode = ?";
		int i = Connect.executeDML(sql, c.getIllName(),c.getPyCode(),c.getWbCode(),c.getIllCode());
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据illCode删除慢病
	 */
	@Override
	public boolean deleteIll(String illCode) {
		//创建SQL语句
		String sql = "delete from chronicdis where illcode = ?";
		int i = Connect.executeDML(sql,illCode);
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

}
